function startGame() {
  //定义DOM节点
  var addOne = document.getElementById("one");
  var addSymbol = document.getElementById("add");
  var addTwo = document.getElementById("two");
  var btnDui = document.getElementById("dui");
  var btnCuo = document.getElementById("cuo");
  var wenhao = document.getElementById("wenhao");
  var guanShu = document.getElementById("gs");
  var s = document.getElementById("djs");
  var musicDui = document.getElementById("musicDui");
  var musicCuo = document.getElementById("musicCuo");
  var guanShuNumber = guanShu.innerText;

  guanShuNumber = 1;
  guanShu.innerHTML = guanShuNumber;
  //console.log("初始化关数" + guanShuNumber);

  function forceSafariPlayAudio() {
    //使用微信jssdk重新加载资源，解决IOS上Bug
    document.addEventListener("WeixinJSBridgeReady", function () {
      musicDui.load(); // iOS 9   还需要额外的 load 一下, 否则直接 play 无效
      musicCuo.load(); // iOS 9   还需要额外的 load 一下, 否则直接 play 无效
    }, false);
  }
  forceSafariPlayAudio();


  //倒计时进度条
  function animateProcess() {
    //当关数大于40时，动画时间变短
    var processTime;
    if (guanShu.innerHTML >= 40) {
      processTime = 2000
    } else {
      processTime = 3000
    }
    $("#djs-progress").animate({
      width: 0 + "%"
    }, processTime, processBack());

    function processBack() {
      $("#djs-progress").css('width', 100 + "%");
    }
  }
  animateProcess();



  var myinterval = window.setInterval(run, 100);

  function run() {
    s.innerHTML = parseFloat(s.innerHTML * 1 - 0.1).toFixed(1);
    //console.log(s.innerHTML / 0.03+'%');
    if (s.innerHTML == 0) {
      //console.log("时间到了");
      clearInterval(myinterval);
      clearInterval(usedTime);
      saveHighestRecord();
      console.log("时间到了，游戏所用时间" + t);
      //            $.alert("时间到了" + "当前关数：" + guanShuNumber, function () {
      //                //点击确认后的回调函数
      //                $("#gameOver").hide();
      //                $("#record").hide();
      //                $("#game").hide();
      //                $("#index").show();
      //                s.innerHTML = 3;
      //            });
      $("#game").hide();
      $("#record").hide();
      $("#index").hide();
      $("#gameOver").show();
      $("#sbguanshu").html(guanShuNumber);
      s.innerHTML = 3;
    }
  }
  //倒计时结束

  //统计游戏用时
  var t = 0;
  var usedTime = window.setInterval(usetime, 100);

  function usetime() {
    t = parseFloat(t * 1 + 0.1).toFixed(1);
    //console.log(t);
  }

  //鼓励机制
  function guli() {
    var guliTime = t / guanShuNumber;
    console.log("t*********" + t);
    console.log("guanShuNumber*********" + guanShuNumber);
    console.log("guliTime*********" + guliTime);
    if (guliTime <= 1) {
      if (guanShuNumber % 5 == 0) {
        $.toptip('你很棒哦！', 800, 'success');
      }
    } else {
      if (guanShuNumber % 5 == 0) {
        $.toptip('要加油了哦！', 800, 'success');
      }
    }
  }

  //大于一定关数后倒计时变短函数
  function timeShort(timeGuanShu, second) {
    if (guanShuNumber >= timeGuanShu) {
      s.innerHTML = second;
    } else {
      s.innerHTML = 3;
    }
  }

  //关数增加函数
  function addguanshu() {
    guanShu.innerHTML = parseInt(guanShuNumber) + 1;
    guanShuNumber = guanShu.innerHTML;
    //console.log("guanShu.innerText" + guanShu.innerText);
  }

  function jixu() {
    mainFun();
    addguanshu();
    guanShuNumber = guanShu.innerText;
    timeShort(40, 2);
    run();
    $("#djs-progress").stop();
    animateProcess();
    guli();
    s.innerHTML = 3;
  }

  //答对时触发的函数
  function answerTrue() {
    if (guanShu.innerText == 20) {
      console.log("关数关数" + guanShuNumber);
      s.innerHTML = 8;
      $("#djs-progress").stop();
      musicCuo.pause();
      musicDui.play();
      musicDui.currentTime = 0;
      $.alert("休息5秒钟吧，接下来难度增加了哦！点击下方确定按钮跳过休息。", "达到20关了", function () {
        jixu();
        clearTimeout(fiveSecond);
      });
      var fiveSecond = setTimeout(function () {
        $.closeModal();
        jixu();
      }, 5000);
    } else {
      console.log("关数关数elseesle" + guanShuNumber);
      musicCuo.pause();
      musicDui.play();
      musicDui.currentTime = 0;
      mainFun();
      guanShuNumber = guanShu.innerText;
      addguanshu();
      timeShort(40, 2);
      run();
      $("#djs-progress").stop();
      animateProcess();
      guli();
    }
  }
  //答错误时触发的函数
  function answerFalse() {
    guanShuNumber = guanShu.innerText;
    //console.log("答错关数" + guanShuNumber);
    guanShuNumber = parseInt(guanShuNumber);
    musicDui.pause();
    musicCuo.play();
    musicCuo.currentTime = 0;
    clearInterval(myinterval);
    clearInterval(usedTime);
    console.log("游戏所用时间" + t);
    $("#djs-progress").stop();
    saveHighestRecord();
    //console.log("gamescore"+guanShu.innerText);

    //        $.alert("答错了!!" + "当前关数：" + guanShuNumber, function () {
    //            //点击确认后的回调函数
    //            musicCuo.pause();
    //            $("#game").hide();
    //            $("#record").hide();
    //            $("#index").hide();
    //            $("#gameOver").show();
    //            s.innerHTML = 3;
    //        });
    //musicCuo.pause();
    $("#game").hide();
    $("#record").hide();
    $("#index").hide();
    $("#gameOver").show();
    $("#sbguanshu").html(guanShuNumber);
    s.innerHTML = 3;
  }

  /**
   *保存最高记录数据
   */
  var saveHighestRecord = function () {
    var game = {};
    game.app = 'jjtz';
    var score = guanShu.innerText;
    var lever = guanShu.innerText;
    var time = t;
    game.score = score;
    game.lever = lever;
    game.time = time;
    console.log("game.score" + game.score);
    console.log("game.lever" + game.lever);
    console.log("game.time" + game.time);
    // page.apiPost('/index/game/saveHighestRecord', game, function (res) {
    //   if (res.code === 0) {
    //     console.log(res);
    //   } else {
    //     webapp.alert(res.msg);
    //   }
    // });

  };

  //主函数
  function mainFun() {
    var s = document.getElementById("djs");
    var ranNumber = parseInt(10 * Math.random());
    console.log("随机数" + ranNumber);
    //第一个随机数
    var value1 = parseInt(10 * Math.random());
    //第二个随机数
    var value2 = parseInt(10 * Math.random());

    //关数设置 不同关数时 两个相加数的值
    if (guanShuNumber >= 1 && guanShuNumber < 20) {
      value1 = parseInt(10 * Math.random());
      value2 = parseInt(10 * Math.random());
    } else if (guanShuNumber >= 20 && guanShuNumber < 40) {
      value1 = parseInt(10 * Math.random()) + parseInt(5 * ranNumber) + 10;
      value2 = parseInt(10 * Math.random()) + parseInt(5 * ranNumber) + 12;
    } else if (guanShuNumber >= 40 && guanShuNumber < 80) {
      value1 = parseInt(90 * ranNumber) + 105;
      value2 = parseInt(90 * ranNumber) + 100;
      ranNumber = ranNumber + 1;
    } else if (guanShuNumber >= 80 && guanShuNumber < 1000) {
      value1 = parseInt(10 * Math.random()) + parseInt(100 * ranNumber) + 101;
      value2 = parseInt(10 * Math.random()) + parseInt(100 * ranNumber) + 109;
    }
    //设置两个相加数的值
    addOne.innerHTML = value1;
    addTwo.innerHTML = value2;
    //正确答案


    //增加减法---通过判断关数模式
    // if (guanShuNumber > 10 && guanShuNumber < 18) {
    //   addSymbol.innerHTML = "-";
    //   ans = value1 - value2;
    //   wenhao.innerHTML = ans - ranNumber;
    //   console.log("减法：" + wenhao.innerHTML);
    // } else {
    //   console.log("else else")
    //   addSymbol.innerHTML = "+";
    //   ans = value1 + value2;
    //   wenhao.innerHTML = ans + ranNumber;
    // }

    //增加减法---通过判断随机数模式
    if (ranNumber > 5 && ranNumber < 10) {
      addSymbol.innerHTML = "-";
      ans = value1 - value2;
      wenhao.innerHTML = ans - ranNumber;
      //console.log("减法：" + wenhao.innerHTML);
    } else {
      //console.log("else else")
      addSymbol.innerHTML = "+";
      ans = value1 + value2;
      wenhao.innerHTML = ans + ranNumber;
    };

    //设置正确率 当随机出7、4、9时，重置为0，即正确答案
    if (ranNumber == 1 || ranNumber == 3 || ranNumber == 5 || ranNumber == 6 || ranNumber == 8) {
      ranNumber = 0;
    }


    //设置问号处的值
    wenhao.innerHTML = ans + ranNumber;
    //console.log(value1);
    //console.log(value2);
    btnDui.onclick = function () {
      if (ans == ans + ranNumber) {
        //alert("答对了");
        answerTrue();
      } else {
        //答错了
        answerFalse();
      }
    };
    btnCuo.onclick = function () {
      if (ans == ans + ranNumber) {
        //答错了
        answerFalse();
      } else {
        //alert("答对了");
        answerTrue();
      }
    }

  }
  //主函数结束

  mainFun();
}